package com.barnaby.sudoku;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.AlertDialog;
import android.content.DialogInterface;


public class Sudoku extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findViewById(R.id.continue_button).setOnClickListener(this);
		findViewById(R.id.new_button).setOnClickListener(this);
		findViewById(R.id.about_button).setOnClickListener(this);
		findViewById(R.id.exit_button).setOnClickListener(this);
		findViewById(R.id.experiment_button).setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
		case R.id.about_button:
			startActivity(new Intent(this, About.class));
			break;
		case R.id.new_button:
			openNewGameDialog();
			break;
		case R.id.exit_button:
			finish();
			break;
		case R.id.experiment_button:
			startActivity(new Intent(this, Experiment.class));
			break;
		}
		
	}
	
	private static final String TAG = "Sudoku";
	private void openNewGameDialog(){
		new AlertDialog.Builder(this)
			.setTitle(R.string.new_game_title)
			.setItems(R.array.difficulty, 
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							startGame(which);}
						}).show();
	}
	
	private void startGame(int i){
		Log.d(TAG, "clicked on " + i);
		Intent intent = new Intent(this, GameActivity.class);
		intent.putExtra(GameActivity.KEY_DIFFICULTY, i);
		startActivity(intent);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case R.id.settingsMenuItem:
			startActivity(new Intent(this, Preferences.class));
			return true;
			
		default:
			return false;
		}
		
	}
	

}
